<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Example
 *
 * This is an example of a few basic user interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 * @link		http://philsturgeon.co.uk/code/
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class market extends REST_Controller
{    //$name, $type, $description, 
	function market_put()
    {
    	if(!$this->put('market_type_name') &&
    	   !$this->put('market_name') &&
    	   !$this->put('market_currency_name'))
    	{
    		$this->response(array('status'=>"false", 'message' => 'Lost input'), 200); // 200 being the HTTP response code
       	}
    	$this->load->model('marketdao');
    	$market = $this->marketdao->create(
    			$this->put('market_name'),
    			$this->put('market_type_name'),
    			$this->put('market_description'),
    			$this->put('market_picture_url'),
    			$this->put('market_email'),
    			$this->put('market_currency_name'));
    	if($market != 0){
    		$this->response(array('result'=>$market,'status'=>"true"), 200); // 200 being the HTTP response code
    	} else {
    		$this->response(array('status'=>"false"), 200); // 200 being the HTTP response code
    	}
    }

	function market_delete() 
	{
		if(!$this->delete('market_id')) {
			$this->response(array('status'=>"false", 
					'message' => 'market_id not provided'), 200);
		}
		$this->load->model('marketdao');
		$success = $this->marketdao->del(
				$this->delete('market_id'));
		
		$this->response(array('status'=> $success ? 'true' : 'false'), 200);
	}
	
	function address_get()
	{
		if(!$this->get('neighborhood_id')) {
			$this->response(array('status'=>"false",
					'message' => 'neighborhood_id not provided'), 200);
		}
		$this->load->model('marketdao');
		$success = $this->marketdao->get_by_address($this->get('neighborhood_id'));
		$this->response(array('markets'=>$success,'status'=> $success ? 'true' : 'false'), 200);
	}
	
	function getbyid_get()
	{
		if(!$this->get('market_id'))
		{
			$this->response(NULL, 400);
		}
		$this->load->model('marketdao');
		$market = $this->marketdao->getbyid($this->get('market_id'));
		$this->response(array('markets'=>$market,'status'=> $market ? 'true' : 'false'), 200);
	}
	
	function getbyemail_get()
	{
		if(!$this->get('market_email'))
		{
			$this->response(NULL, 400);
		}
		$this->load->model('marketdao');
		$market = $this->marketdao->getbyemail($this->get('market_email'));
		$this->response(array('markets'=>$market,'status'=> $market ? 'true' : 'false'), 200);
			}
	
	function getmarkets_get()
	{
		$this->load->model('marketdao');
		$markets = $this->marketdao->getmarkets();
		$this->response(array('markets'=>$markets,'status'=> $markets ? 'true' : 'false'), 200);
		
	}
}